package com.hexb.smh.mapper;

import com.hexb.smh.entity.Product;
import com.hexb.smh.entity.ProductRaw;
import com.hexb.smh.entity.param.product.ProductQueryParam;
import com.hexb.smh.entity.view.ProductBrandView;
import com.hexb.smh.entity.view.ProductClientView;
import com.hexb.smh.entity.view.ProductSumView;
import com.hexb.smh.entity.view.ProductView;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author : hexb
 * BaseMapper内置方法部分仅支持mysql
 */
@Mapper
@Repository
public interface ProductMapper {

    //保存新产品
    int save(Product account);

    int batchSaveOrUpdate(List<Product> products);

    int saveOrUpdate(Product product);

    int clear();

    //查询产品列表(有级联价格)
    List<ProductView> search(ProductQueryParam param);

    //查询产品列表(主要用于评估时的列表)
    List<ProductBrandView> findWithBrandId(@Param("brandId") Integer brandId,
                                           @Param("ids") Integer[] ids,
                                           @Param("activated") Boolean activated,
                                           @Param("searchKey") String searchKey);

    //更新已有产品
    int updateByEntity(Product product);

    ProductView selectById(Integer id);

    @Select("select * from product where itemNo=#{itemNo} limit 1 offset 0")
    ProductRaw findByItemNo(@Param("itemNo") String itemNo);

    @Delete("delete from product where id=#{id}")
    int delete(Integer id);

    //批量删除
    int deleteBatch(@Param("ids") Integer[] ids);

    ProductRaw findOneByMap(Map<?, ?> param);

    int updateActivatedBatch(@Param("ids") Integer[] ids, @Param("activated") Boolean activated);

    List<ProductClientView> searchClient(@Param("searchKey") String searchKey,
                                         @Param("brandId") Integer brandId);

    /**
     * 汇总选中产品的各种价格
     */
    ProductSumView sum(@Param("ids") Integer[] ids, @Param("brandId") Integer brandId);

    List<Product> listByIds(@Param("ids") Integer[] ids);
}
